package servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Databasebean;

public class DatabaseServlet extends HttpServlet {

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
	String typeString = request.getParameter("type");
	if (typeString.equals("check")) {
	    String dirString = request.getParameter("backDir");
	    if ( dirString != null && !"".equals(dirString)) {
		File file = new File(dirString);
		if (!file.isDirectory()) {
		    response.getWriter().write("notExists");
		}else{
		    response.getWriter().write("exists");
		}
	    }
	}else if(typeString.equals("backup")){
	    String dirString = request.getParameter("backDir");	    
	    if (!"".equals(dirString) && dirString != null) {
		response.getWriter().write(dirString);
		Databasebean db = new Databasebean();
		if(db.backup(dirString)){
		    request.setAttribute("success", "true");
		}else{
		    request.setAttribute("success", "false");
		}
		request.getRequestDispatcher("../admin/database.jsp").forward(request, response);
	    }
	}
    }
}
